package cybertwin

import "sync"

type scheduler struct {
	// 请求队列
	Req_Queue [PRIORITY_TYPE]chan Request_Record
	//Req_Queue_Mu [PRIORITY_TYPE]sync.RWMutex

	// 响应队列
	Resp_Queue [PRIORITY_TYPE]chan Response_Record
	//Resp_Queue_Mu [PRIORITY_TYPE]sync.RWMutex

	// 响应缓存
	Resp_Buffer map[BufferKey]*Response_Record

	// for OnTraffic
	ReqOrResp int

	// for Multi Go routine
	Req_routine_num_mu  *sync.Mutex
	Req_routine_num     int
	Req_max_routine_num int

	Resp_routine_num_mu  *sync.Mutex
	Resp_routine_num     int
	Resp_max_routine_num int
}

func (sch *scheduler) Init() {

}

// 对请求队列进行一次调度，返回一个Request_Record结构体
func (sch *scheduler) request_scheduler_once() (req_ptr *Request_Record, sched_succ bool) {
	var request Request_Record
	sched_succ = false
	for i := 0; i < PRIORITY_TYPE; i++ {
		//cbt.Req_Queue_Mu[i].Lock()
		if len(sch.Req_Queue[i]) > 0 {
			request = <-sch.Req_Queue[i]
			sched_succ = true
			//cbt.Req_Queue_Mu[i].Unlock()
			break
		}
		//cbt.Req_Queue_Mu[i].Unlock()
	}

	if !sched_succ {
		return
	}

	req_ptr = &request
	return
}

// 对响应队列进行一次调度，返回一个Response_Record结构体
func (sch *scheduler) response_scheduler_once() (resp_ptr *Response_Record, sched_succ bool, err error) {
	var response Response_Record
	sched_succ = false
	for i := 0; i < PRIORITY_TYPE; i++ {
		//cbt.Resp_Queue_Mu[i].Lock()
		if len(sch.Resp_Queue[i]) > 0 {
			response = <-sch.Resp_Queue[i]
			sched_succ = true
			//cbt.Resp_Queue_Mu[i].Unlock()
			break
		}
		//cbt.Resp_Queue_Mu[i].Unlock()
	}
	if !sched_succ {
		return
	}

	resp_ptr = &response
	return
}
